layui.use(['form','layer','jquery','laypage','table'],function(){
    var layer=layui.layer,table=layui.table,form=layui.form,$=layui.jquery;

    var tableoperation={
         start:function (id) {
            $.ajax({
                url: "/iot/dispatch/start/"+id,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                             table.reload("dispatchlist");
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
        ,view :function (id) {
            layer.open({
                title : "详情",
                area: ['90%', '90%'],
                offset: 't',
                fix: false,
                maxmin: true,
                shadeClose: false,
                shade: 0.4,
                type : 2,
                content : "/iot/dispatch/view/"+id
                ,cancel:function (index,layero) {
                    /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                   table.reload("dispatchlist");
                }
            })
        }
        ,cancel:function (id) {
            $.ajax({
                url: "/iot/dispatch/cancel/"+id,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                           table.reload("dispatchlist");
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
    }

    function openLayer(clspath){
        layer.open({
            title : "上传文件",
            area: ['90%', '90%'],
            offset: 't',
            fix: false,
            maxmin: true,
            shadeClose: false,
            shade: 0.4,
            type : 2,
            content : "/iot/manager/uploadcls?clspath="+clspath
            ,cancel:function (index,layero) {
                /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                location.reload();
            }
        })
    };


    /*------------------------------*/
    //方法级渲染
    var tabdispatch={
        id: 'dispatchlist',
        elem: '#dispatch'
        ,width:'100%'
        /* ,loading: true*/
        ,cellMinWidth: '80'
        , url: '/iot/dispatch/listPage'
        ,defaultToolbar: ['exports','filter','print']
        ,toolbar:'#toolDispatchDemo'
        , cols: [[
            {type:'checkbox', fixed:'left'}
            , {field: 'DEVICENAME', title: '所属设备',width:'160'}
            , {field: 'MSGSOURCE',title: '消息来源',width:'160',sort:true}
            , {field: 'ADDRESS', title: '分发目的ip',width:'160'}
            , {field: 'EXCHANGE', title: '交换机',width:'160'}
            , {field: 'QUEUENAME',title:'分发队列',width:'160'}
            , {field: 'ISOPEN',title:'是否禁用',width:'160',templet:function(d){
                    if (d.ISOPEN =='1'){ return '启用';}else {
                        return '禁用';
                    }
                }}
            , {field: 'right', title: '操作',toolbar: "#bardispatchDemo",fixed:'right',width:'300'}
        ]]
        , page: true
        , request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusCode: 200 //成功的状态码，默认：0
            , countName: 'count' //数据总数的字段名称，默认：count
            , dataName: 'data' //数据列表的字段名称，默认：data
        }
        ,even: true
    };
    table.render(tabdispatch);

    /*
  * 监听行事件
  * */
    table.on('tool(dispatchlist)',function (obj) {
        var data=obj.data;
        console.log(data);
        tableoperation[obj.event](data.ID);
    });

    //监听头事件
    table.on('toolbar(dispatchlist)', function(obj){
        console.log(obj);
        var checkStatus = table.checkStatus(obj.config.id);
        console.log(checkStatus);
        var data = checkStatus.data;
        console.log(data);
        switch(obj.event){
            case 'add_dispatch':
                layer.open({
                    title : "添加消息分发",
                    area: ['90%', '90%'],
                    offset: 't',
                    fix: false,
                    maxmin: true,
                    shadeClose: false,
                    shade: 0.4,
                    type : 2,
                    content : "/iot/dispatch/addpage"
                    ,cancel:function (index,layero) {
                        /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                        table.reload('dispatchlist');
                    }
                });
                break;
        };
    });



})
